﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Security;
using Meqs.Entity;
using Meqs.Logic.Interface;
using Castle.Facilities.IBatisNetIntegration;
using Castle.Services.Transaction;
using System.Web;

namespace Meqs.Logic.Implement
{
    [Transactional]
    [UsesAutomaticSessionCreation]
    public class EducationUsersLogic : CommonLogic<EducationUsers, int>, IEducationUsersLogic
    {
        private readonly IEduPremLogic _eduPrmLogic;

        public EducationUsersLogic(IEduPremLogic eduPrmLogic)
        {
            _eduPrmLogic = eduPrmLogic;
        }

        #region Implementation of ICommonLogic<EducationUsers, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(EducationUsers entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(EducationUsers entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public EducationUsers FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<EducationUsers> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<EducationUsers> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion



        /// <summary>
        /// 插入用户，并插入相应的权限
        /// </summary>
        /// <param name="edu"></param>
        /// <param name="iRights"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int InsertUser(EducationUsers edu, List<EduPrem> iRights)
        {
            //增加用户
            int iResult = Create(edu);

            //增加权限
            if (iResult > 0)
            {
                foreach (EduPrem right in iRights)
                {
                    right.EduUserID = iResult;

                    _eduPrmLogic.InsertUser(right);
                    //InsertCustomer("EduPrem.Insert", right);
                }

                return iResult;
            }
            else
                return -1;

        }

        /// <summary>
        /// 重新设置密码
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="RIDS"></param>
        /// <returns></returns>
        public int ResetPwd(string pwd, List<int> RIDS)
        {
            int iResult = 0;

            foreach (int RID in RIDS)
            {
                Hashtable ht = new Hashtable();
                ht.Add("RID", RID);
                ht.Add("Passwd", pwd);

                iResult = ExecuteUpdate("EducationUsers.ResetPwd", ht);
            }

            return iResult;
        }

        /// <summary>
        /// 修改用户基本信息，不包括密码
        /// </summary>
        /// <param name="edu"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int UpdateUser(EducationUsers edu)
        {
            try
            {
                return ExecuteUpdate("EducationUsers.UpdateUser", edu);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改用户（不包括密码）及相应的用户权限
        /// </summary>
        /// <param name="edu"></param>
        /// <param name="iRights"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int UpdateUser(EducationUsers edu, List<EduPrem> iRights)
        {
            //修改用户
            int iResult = UpdateUser(edu);

            if (iResult > 0)
            {
                //根据用户ID删除所有权限
                _eduPrmLogic.Delete(edu.RID);

                //重新增加权限
                foreach (EduPrem right in iRights)
                {
                    _eduPrmLogic.InsertUser(right);
                }

                return iResult;
            }
            else
                return -1;

        }

        /// <summary>
        /// 删除List里面的所有用户
        /// </summary>
        /// <param name="iRIDs"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int DeleteUser(List<int> iRIDs)
        {
            foreach(int RID in iRIDs)
            {
                Delete(RID);
            }
            return 1;
        }

        /// <summary>
        /// 根据条件来获取用户列表
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public IList<EducationUsers> GetByCondition(Hashtable ht)
        {
            try
            {
                string statementName = "EducationUsers.FindByCondition";
                return ExecuteQueryForList<EducationUsers>(statementName, ht);
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 登陆验证
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="pass">密码</param>
        /// <param name="Area">学区</param>
        /// <returns>登陆状态</returns>
        public int CheckLogin(string userName, string pass,int Area)
        {
            Perm perm = new Perm();
            Hashtable ht=new Hashtable();
            ht.Add("UserName", userName);
            ht.Add("AreaID", Area);
            IList<EducationUsers> list=FindObject(ht);
            if (list.Count <= 0)
            {
                return 1;//用户名错误
            }
            if (list[0].Passwd != pass)
            {
                return 2;//密码错误
            }
            perm.UID = list[0].RID;
            perm.UName = list[0].UserActualName;
            perm.UserName = list[0].UserName;
            perm.SchoolAreaId = Area;
            if (Area > 0)
            {
                ht.Clear();
                ht.Add("RID", Area);
                SchoolArea schoolArea = ExecuteQueryForObject<SchoolArea>("SchoolArea.FindById", ht);
                if (schoolArea != null)
                {
                    perm.SchoolAreaName = schoolArea.SchoolAreaName;
                }
            }
            else
            {
                perm.SchoolAreaName = "教育局";

            }

            if (list[0].IsAdmin == 1 && Area == 0)//局级管理员
            {
                perm.UserType = 1;
               
            }
            else
            {
                perm.UserType = 2;
                ht.Clear();
                ht.Add("EduUserID", list[0].RID);
                IList<EduPrem> permList = this.ExecuteQueryForList<EduPrem>("EduPrem.Find", ht);
                foreach (EduPrem eduPrem in permList)
                {
                    if (eduPrem.PNo == 31)
                    {
                        perm.Edu_Exam = true;
                    }
                    if (eduPrem.PNo == 32)
                    {
                        perm.Edu_Report = true;
                    }
                }
            }

            HttpCookie areaCookie= new HttpCookie("Area", Area.ToString());
            areaCookie.Expires = DateTime.Now.AddMonths(12);
            HttpContext.Current.Response.AppendCookie(areaCookie);
            HttpCookie loginWayCookie = new HttpCookie("LoginWay", "3");
            loginWayCookie.Expires = DateTime.Now.AddMonths(12);
            HttpContext.Current.Response.AppendCookie(loginWayCookie);
            
            HttpContext.Current.Session["UserInfo"] = perm;
            //登陆成功
            return 3;

        }
    }
}
